package com;


import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * @author CKC
 * @DateTime 2022/2/27 13:58
 */
public class CglibDemo {
    static class Sample {
        private String param1;


        public void test1() {
            System.out.println("hello World 1!");
        }

        public void test2() {
            System.out.println("hello World 2!");
        }
    }

    public static void main(String[] args) {
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(Sample.class);

        enhancer.setCallback(new MethodInterceptor() {
            @Override
            public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
                System.out.println("before method run...");
                Object result = proxy.invokeSuper(obj, args);
                System.out.println("after method run...");
                return result;
            }
        });
        Sample sample = (Sample) enhancer.create();
        sample.test1();
    }
}
